package com.community.common.util;

import cn.hutool.core.lang.Snowflake;
import com.community.common.config.OssProperty;
import com.community.common.pojo.Constant;
import com.community.common.pojo.domain.UserPermission;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;

/**
 * @author Administrator
 */
@Slf4j
@Component
public class CommonUtil {

    private static Snowflake SNOWFLAKE;
    private static OssProperty OSSPROPERTY;

    @Resource(name = "mySnowflake")
    private Snowflake snowflake;

    @Resource
    private OssProperty ossProperty;

    @PostConstruct
    public void init() {
        SNOWFLAKE = snowflake;
        OSSPROPERTY = ossProperty;
    }

    /**
     * @return 获取雪花ID
     */
    public static long getId() {
        return SNOWFLAKE.nextId();
    }

    public static String getIdStr() {
        return String.valueOf(SNOWFLAKE.nextId());
    }

    public static Date getNow() {
        return new Date();
    }

    /**
     * @param timeMillis 时间戳
     * @return 时间对象
     */
    public static Date getTime(long timeMillis) {
        return new Date(timeMillis);
    }

    public static UserPermission buildUserPermission(String relevanceId,String communityId,String cameraId) {
        return UserPermission.builder()
                .id(String.valueOf(CommonUtil.getId()))
                .currentAllow(0)
                .targetAllow(0)
                .cameraId(cameraId)
                .commandStatus(Constant.COMMAND_INIT)
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .communityId(communityId)
                .idTable(Constant.TABLE_PUBLIC)
                .relevanceId(relevanceId)
                .build();
    }

    /**
     * @return 获取5位随机数
     */
    public static String getRandom() {
        return String.valueOf(Math.round(Math.random() * 100000));
    }

    public static String getOssFileUrl(String fileName) {
        return StringUtils.join(OSSPROPERTY.url, "/img/user/", fileName, OSSPROPERTY.parameter);
    }

    public static String getOssFileName(String fileName) {
        return StringUtils.join("img/user/", fileName);
    }

    public static String getOssRecordFileUrl(String fileName) {
        return StringUtils.join(OSSPROPERTY.url, "/img/record/", fileName, OSSPROPERTY.parameter);
    }

    public static String getOssRecordFileName(String fileName) {
        return StringUtils.join("img/record/", fileName);
    }

    public static String getOssRecordFileNameFromUrl(String url) {
        return url.replace(OSSPROPERTY.url, "").replace(OSSPROPERTY.parameter, "");
    }

    public static String getOldOssFileName(String url) {
        if (!url.contains("user")) {
            log.warn("URL：{} 中不包含 user 为非法路径", url);
            return null;
        }
        return url.split("user")[1];
    }

    public static void main(String[] args) {
        System.out.println("http://buc0001.oss-cn-hangzhou.aliyuncs.com/img/03.jpg".split("img")[1]);
    }

}
